/* Rafael Setra
 * Software Design Period 8
 * This program is the main game class and will incorporate the other games and give the user a choice
 * between what game to play.
 */

import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class BrainGames extends JApplet implements ActionListener
{
	Game currentGame = new Game();
	JButton dragPlay = new JButton("Click to Play the DragDrop game");
	JButton ahaPlay = new JButton("Click to Play the Disappearing game");
	JButton numberPlay = new JButton("Click to Play the Numbers game");
	JPanel playPanel = new JPanel();
	JButton menu = new JButton("Click to return to Menu");
	ArrayList<Image> pics = new ArrayList<Image>();
	ArrayList<Image> pics2 = new ArrayList<Image>();
	JPanel gameSelection= new JPanel(new GridLayout(3,3));//drag
	JPanel gameSelection2 = new JPanel(new GridLayout(1,2));//aha
	JPanel gameSelection3 = new JPanel(new GridLayout(1,1));//number
	ArrayList<JButton> dragButtons = new ArrayList<JButton>();
	ArrayList<JButton> ahaButtons = new ArrayList<JButton>();
	JButton ahaButton;
	JButton numberButton = new JButton("NUMBER");
	Boolean start = true;
	
	//initializes buttons and picture datas
	public void init()
	{
		pics.add(getImage(getCodeBase(),"nighttime.jpg"));
		pics.add(getImage(getCodeBase(),"colorfulstuff.jpg"));
		pics.add(getImage(getCodeBase(),"flower1.jpg"));
		pics.add(getImage(getCodeBase(),"flower2.jpg"));
		pics.add(getImage(getCodeBase(),"magnetgirl1.jpg"));
		pics.add(getImage(getCodeBase(),"flowers3.jpg"));
		pics.add(getImage(getCodeBase(),"NewYork.jpg"));
		pics.add(getImage(getCodeBase(),"pikachu.jpg"));
		pics.add(getImage(getCodeBase(),"Pic2A-1.jpg"));
		
		pics2.add(getImage(getCodeBase(),"PictureSample-1.jpg"));
		pics2.add(getImage(getCodeBase(),"PictureSample_2-1.jpg"));
		pics2.add(getImage(getCodeBase(),"Pic2A-1.jpg"));
		pics2.add(getImage(getCodeBase(),"Pic2B-1.jpg"));
		for(int i = 0;i<pics.size();i++)
		{
			dragButtons.add(new JButton("", new ImageIcon(pics.get(i))));
			dragButtons.get(i).setActionCommand(""+i);
			dragButtons.get(i).addActionListener(this);
		}//end for
		for(int i = 0;i<pics2.size();i++)
		{
			ahaButtons.add(new JButton("", new ImageIcon(pics2.get(i))));
			ahaButtons.get(i).setActionCommand("a"+i);
			ahaButtons.get(i).addActionListener(this);
		}//end for
		menu.addActionListener(this);
		setSize(1000,900);
		setLayout(new BorderLayout());
//		Container c = playPanel.getRootPane();
		playPanel.setLayout(new GridLayout(2,2));
		dragPlay.addActionListener(this);
		dragPlay.setVisible(true);
		ahaPlay.addActionListener(this);
		ahaPlay.setVisible(true);
		numberPlay.addActionListener(this);
		numberPlay.setVisible(true);
		playPanel.setVisible(true);
		for(int i = 0;i<dragButtons.size();i++)
			gameSelection.add(dragButtons.get(i));
		for(int i = 0;i<ahaButtons.size()/2;i++)
			gameSelection2.add(ahaButtons.get(i*2));
		numberButton.addActionListener(this);
		gameSelection3.add(numberButton);
		playPanel.add(ahaPlay);
		playPanel.add(dragPlay);
		playPanel.add(numberPlay);
		getContentPane().add(playPanel,BorderLayout.NORTH);
	}//end init
	
	public void update(Graphics g)
	{
		
	}//end update
	
	public void paint(Graphics g)
	{	
		if(start)
		{
			playPanel.setVisible(false);
			ahaPlay.setVisible(false);
			dragPlay.setVisible(false);
			playPanel.setVisible(true);
			ahaPlay.setVisible(true);
			dragPlay.setVisible(true);
		}//end if
		start=false;	
	}//end paint
	
	public void actionPerformed(ActionEvent e) 
	{
		// TODO Auto-generated method stub
		if(e.getActionCommand().equals(dragPlay.getActionCommand()))
		{
			removeAll();
			addDragGames();
		}//end if
		if(e.getActionCommand().equals(ahaPlay.getActionCommand()))
		{
			removeAll();
			addAhaGames();
		}//end if
		if(e.getActionCommand().equals(numberPlay.getActionCommand()))
		{
			removeAll();
			addNumberGames();
		}//end if
		
		if(e.getActionCommand().equals(menu.getActionCommand()))
		{
			menu();
		}//end if
		
		for(int i =0;i<dragButtons.size();i++)
		{
			if(e.getActionCommand().equals(dragButtons.get(i).getActionCommand()))
			{
				dragGameStart(dragButtons.get(i));
				break;
			}//end if
		}//end for
		
		for(int i =0;i<ahaButtons.size();i++)
		{
			if(e.getActionCommand().equals(ahaButtons.get(i).getActionCommand()))
			{
				ahaGameStart(ahaButtons.get(i),i);
				break;
			}//end if
		}//end for
		
		if(e.getActionCommand().equals(numberButton.getActionCommand()))
		{
			numberGameStart();
		}//end if
		
	}//end actionPerformed

	
	//starts a NumberGame
	public void numberGameStart()
	{
		gameSelection3.setVisible(false);
		currentGame = new NumberGame();
		getContentPane().add(currentGame,BorderLayout.CENTER);
		currentGame.setVisible(true);
	}//end numberGameStart
	
	//starts a dragGame
	public void dragGameStart(JButton selection)
	{
		gameSelection.setVisible(false);
		currentGame = new DraggingGame(((ImageIcon) selection.getIcon()).getImage(),5,5);
		getContentPane().add(currentGame,BorderLayout.CENTER);
		currentGame.setVisible(true);
	}//end dragGameStart
	
	//starts an AhaGame
	public void ahaGameStart(JButton selection, int i)
	{
		gameSelection2.setVisible(false);
		currentGame = new AhaChange(((ImageIcon) selection.getIcon()).getImage(),((ImageIcon) ahaButtons.get(i+1).getIcon()).getImage());
		getContentPane().add(currentGame,BorderLayout.CENTER);
		currentGame.setVisible(true);
	}//end ahaGame
		
	//initial menu for the game
	public void menu()
	{
		gameSelection.setVisible(false);
		gameSelection2.setVisible(false);
		gameSelection3.setVisible(false);
		currentGame.setVisible(false);
		menu.setVisible(false);
//		dragPlay.setVisible(true);
		playPanel.setVisible(true);
		
	}//end menu
	
	//removes current games
	public void removeAll()
	{
//		Container c = getContentPane();
//		c.remove(play);
//		dragPlay.setVisible(false);
//		ahaPlay.setVisible(false);
		playPanel.setVisible(false);
		
	}//end removeAll
	
	//adds in a choice of ahaGames to play
	public void addAhaGames()
	{
		Container c = getContentPane();
		c.add(gameSelection2,BorderLayout.CENTER);
		gameSelection2.setVisible(true);
		c.add(menu,BorderLayout.SOUTH);
		menu.setVisible(true);
	}//end addAhaGames
	
	//adds in number games
	public void addNumberGames()
	{
		Container c = getContentPane();
		c.add(gameSelection3,BorderLayout.CENTER);
		gameSelection3.setVisible(true);
		c.add(menu,BorderLayout.SOUTH);
		menu.setVisible(true);
	}//end addNumberGames
	
	//adds in dragging games
	public void addDragGames()
	{
		Container c = getContentPane();
		c.add(gameSelection,BorderLayout.CENTER);
		gameSelection.setVisible(true);
		c.add(menu,BorderLayout.SOUTH);
		menu.setVisible(true);
	}//end addDragGames
}//end class